package com.briup.web.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.domain.Slideshow;
import com.briup.resuit.Result;
import com.briup.service.SlideshowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@Api(tags = "轮播图模块")
@RestController
@RequestMapping("/slideshow")
public class SlideshowController {
    @Autowired
    SlideshowService slideshowService;
    @GetMapping("/queryById/{id}")
    public Result findById(@PathVariable Integer id){
        Slideshow slideshow = slideshowService.getById(id);
        return Result.success(slideshow);
    }
    @GetMapping("/queryAllEnable")
    public Result queryAllEnable() {
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<Slideshow>();
        lqw.eq(Slideshow::getStatus,"启用").orderByDesc(Slideshow::getUploadTime);
        List<Slideshow> list = slideshowService.list(lqw);
        return Result.success(list);
    }
    @ApiOperation("分页条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页",
                    dataType = "int", required = true, defaultValue = "1",
                    paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", required = true, defaultValue = "4",
                    paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态值",
                    paramType = "query"),
            @ApiImplicitParam(name = "desc", value = "描述信息",
                    paramType = "query")
    })

    @GetMapping("/query")
    public Result query(@RequestParam Integer pageNum,@RequestParam Integer pageSize, String
            status, String desc) {
        IPage<Slideshow> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<Slideshow>();
            lqw.eq(status!=null,Slideshow::getStatus,status);
            lqw.like(desc!=null,Slideshow::getDescription,desc);
        IPage<Slideshow> list = slideshowService.page(page, lqw);
        return Result.success(list);
    }
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Slideshow slideshow) {
        slideshow.setUploadTime(new Date());
        slideshowService.saveOrUpdate(slideshow);
        return Result.success("操作成功");
    }
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteSlideshowInBatch(@PathVariable("ids")
                                         List<Integer> ids) {
       slideshowService.removeByIds(ids);
        return Result.success("删除成功");
    }

}
